import { useDictStore } from '@/store/modules/dict';
import { renderImage, renderOptionTag } from '@/utils';
import { validate } from '@/utils/validateUtil';
import { cloneDeep } from 'lodash-es';

const dict = useDictStore();

export class State {
  public id = 0; // ID
  public imgUrl = ''; // 图片
  public name = ''; // 名称
  public price = 0; // 价格(元)
  public type = 1; // 身份
  public cardType = null; // 会员类型
  public exchangeCount = 1; // 权益免费次数
  public memberDays = 0; // 有效天数
  public extraDays = 0; // 额外赠送天数
  public createdAt = ''; // 创建时间
  public updatedAt = ''; // 更新时间
  public status = 1; // 状态：1-正常，2-停用
  public memberProfit = 0; // 会员分润
  public saleProfit = 0;  // 销氪分润
  public serviceProfit = 0; // 服务商分润

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  // imgUrl: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'string',
  //   message: '请输入图片',
  // },
  name: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入名称',
  },
  price: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    validator: validate.amount,
  },
  exchangeCount: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '权益免费次数',
  },
  memberDays: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入有效天数',
  },
  extraDays: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入额外赠送天数',
  },
};

// 表格搜索表单

// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: 50,
  },
  {
    title: '图片',
    key: 'imgUrl',
    align: 'left',
    width: 70,
    render(row: State) {
      return renderImage(row.imgUrl);
    },
  },
  {
    title: '名称',
    key: 'name',
    align: 'left',
    width: 130,
  },
  {
    title: '价格(元)',
    key: 'price',
    align: 'center',
    width: 100,
  },
  {
    title: '身份',
    key: 'type',
    align: 'left',
    width: 90,
    render(row: State) {
      return renderOptionTag('xxb_yh_yhsf', row.type);
    },
  },
  {
    title: '会员类型',
    key: 'cardType',
    align: 'center',
    width: 100,
    render(row: State) {
      return row.type == 2 ? renderOptionTag('xxb_yh_hylx', row.cardType) : '';
    },
  },
  {
    title: '免费次数',
    key: 'exchangeCount',
    align: 'center',
    width: 80,
    render(row: State) {
      return row.type == 2 ? row.exchangeCount : '';
    },
  },
  {
    title: '有效天数',
    key: 'memberDays',
    align: 'center',
    width: 80,
    render(row: State) {
      return row.type == 2 ? row.memberDays : '';
    },
  },
  {
    title: '赠送天数',
    key: 'extraDays',
    align: 'center',
    width: 80,
    render(row: State) {
      return row.type == 2 ? row.extraDays : '';
    },
  },
  {
    title: '上级利润分配(元)',
    align: 'center',
    children: [
      {
        title: '会员',
        key: 'memberProfit',
        align: 'center',
        width: 70,
      },
      {
        title: '销氪',
        key: 'saleProfit',
        align: 'center',
        width: 70,
      },
        {
        title: '服务商',
        key: 'serviceProfit',
        align: 'center',
        width: 70,
      }
    ]
  },

  // {
  //   title: '创建时间',
  //   key: 'createdAt',
  //   align: 'left',
  //   width: 160,
  // },
  // {
  //   title: '更新时间',
  //   key: 'updatedAt',
  //   align: 'left',
  //   width: 160,
  // },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['sys_normal_disable', 'xxb_yh_hylx', 'xxb_yh_yhsf']);
}